﻿/*----------------------------------------------------------------
    // Copyright (C) 2009 江苏华招网
    // 版权所有。 
    // 作者：吕海乐 
    //
    // 文件名：PurchaseOrderAdd_Step2.cs
    // 文件功能描述：新建采购单，新建采购单
    // 创建标识：吕海乐2009-04-27
    //
    // 修改标识：
    // 修改描述：
    //
    // 修改标识：
    // 修改描述：
    //
    // 审核人：
// 审核时间：
// 审核说明：
 //----------------------------------------------------------------*/
using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using HZ.DrugTrade.Model;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.BLLFactory;
using HZ.DrugTrade.Model.Enum;

namespace HZ.DrugTrade.Web.Admin.ProcureManage
{
    public partial class PurchaseOrderAdd_Step2 : BasePage
    {
        public PurchaseOrderAdd_Step2()
        {
            this.ModuleNo = Module.单据管理.ToString();
            this.PageOperateType = OperateType.新建采购单.ToString();
        }

        IBLL_Purchase ip = Factory.BLL_Purchase();

        protected void Page_Load(object sender, EventArgs e)
        {
            string hospitalName = Request.QueryString["hName"];
            if (!IsPostBack)
            {
                txtHospitalName.Text = hospitalName;
                txtPurchaseOrderName.Text = hospitalName + DateTime.Now.ToString("yyyyMMddHHmmss")+"采购单";
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            //检查数据
            if (!string.IsNullOrEmpty(DataCheck()))
            {
                string errormsg = DataCheck();
                HZ.Web.WebUtil.Alert(errormsg,this);
                return;
            }
            //添加采购单
            PurchaseOrder aPurchaseOrder = new PurchaseOrder();
            aPurchaseOrder.HospitalID = Request.QueryString["hID"];
            aPurchaseOrder.OrderID = Guid.NewGuid().ToString();
            aPurchaseOrder.OrderName = txtPurchaseOrderName.Text.Trim();
            aPurchaseOrder.StartTime = new HZ.Common.ConvertHelper(txtPurchaseOrderTimeFrom.Text.Trim()).ToDateTime;
            aPurchaseOrder.PlanEndTime = new HZ.Common.ConvertHelper(txtPurchaseOrderTimeTo.Text.Trim()).ToDateTime;

            aPurchaseOrder.TotalAmount = 0;
            aPurchaseOrder.PlanPerson = CurrentAccount.GetUser().UserID;
            int result=ip.AddPurchaseOrder(aPurchaseOrder);
            if (result == 1)
            {
                Response.Redirect("PurchaseOrderAdd_Step3.aspx?orderID=" + aPurchaseOrder.OrderID + "&hID=" + aPurchaseOrder.HospitalID + "");
            }
            else if (result == 2)
            {
                PageError("采购单存在！", this.ReturnUrl);
            }
            else
            {
                PageError("保存失败！", this.ReturnUrl);
            }
        }

        /// <summary>
        /// 检查数据是否为空及数据的合法性
        /// </summary>
        private string DataCheck()
        {
            string errormsg;
            //采购单名称为空
            if (string.IsNullOrEmpty(txtPurchaseOrderName.Text.Trim()))
            {
                errormsg = "采购单名称不能为空！";
                txtPurchaseOrderName.Focus();
                return errormsg;
            }
            //采购时间为空
            if (string.IsNullOrEmpty(txtPurchaseOrderTimeFrom.Text.Trim()) || string.IsNullOrEmpty(txtPurchaseOrderTimeTo.Text.Trim()))
            {
                errormsg = "采购时间不能为空！";
                txtPurchaseOrderTimeFrom.Focus();
                return errormsg;
            }
            //采购时间晚于结束时间
            if (Convert.ToDateTime(txtPurchaseOrderTimeFrom.Text) > Convert.ToDateTime(txtPurchaseOrderTimeTo.Text))
            {
                errormsg = "采购开始时间晚于结束时间！";
                txtPurchaseOrderTimeFrom.Focus();
                return errormsg;
            }
            return "";
        }
    }
}
